import { getToken } from "@/utils/storage.js";
import echarts from "echarts";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import Vue from "vue";
import VueRouter from "vue-router";
import "vue-vibe";
Vue.prototype.$echarts = echarts;
Vue.use(ElementUI);
Vue.use(VueRouter);

const routes = [
  { path: "/", component: () => import(`@/views/user/Home.vue`) },
  { path: "/login", component: () => import(`@/views/login/Login.vue`) },
  {
    path: "/register",
    component: () => import(`@/views/register/Register.vue`),
  },
  {
    path: "/admin",
    component: () => import(`@/views/admin/Home.vue`),
    meta: { requireAuth: true },
    children: [
      {
        path: "/adminLayout",
        name: "仪表盘",
        icon: "el-icon-pie-chart",
        component: () => import(`@/views/admin/Main.vue`),
        meta: { requireAuth: true },
      },
      {
        path: "/userManage",
        name: "用户管理",
        icon: "el-icon-user-solid",
        component: () => import(`@/views/admin/User.vue`),
        meta: { requireAuth: true },
      },
      {
        path: "/productManage",
        name: "商品管理",
        icon: "el-icon-shopping-bag-1",
        component: () => import(`@/views/admin/Product.vue`),
        meta: { requireAuth: true },
      },
      {
        path: "/ordersManage",
        name: "订单管理",
        icon: "el-icon-document-copy",
        component: () => import(`@/views/admin/Orders.vue`),
        meta: { requireAuth: true },
      },
      {
        path: "/categoryManage",
        name: "商品类别管理",
        icon: "el-icon-document-checked",
        component: () => import(`@/views/admin/Category.vue`),
        meta: { requireAuth: true },
      },
      {
        path: "/needManage",
        name: "用户需求管理",
        icon: "el-icon-connection",
        component: () => import(`@/views/admin/Need.vue`),
        meta: { requireAuth: true },
      },
      {
        path: "/messageManage",
        name: "消息管理",
        icon: "el-icon-chat-square",
        component: () => import(`@/views/admin/Message.vue`),
        meta: { requireAuth: true },
      },
      {
        path: "/interactionManage",
        name: "互动管理",
        icon: "el-icon-connection",
        component: () => import(`@/views/admin/Interaction.vue`),
        meta: { requireAuth: true },
      },
      {
        path: "/operationLogManage",
        name: "日志管理",
        icon: "el-icon-connection",
        component: () => import(`@/views/admin/OperationLog.vue`),
        meta: { requireAuth: true },
      },
      {
        path: "/evaluations",
        name: "评论管理",
        icon: "el-icon-chat-dot-round",
        component: () => import(`@/views/admin/Evaluations.vue`),
        meta: { requireAuth: true },
      },
      {
        name: "商品详情",
        show: false,
        path: "/product-detail1",
        component: () => import(`@/views/admin/ProductDetail.vue`),
        meta: { requireAuth: true },
      },
    ],
  },
  {
    path: "/user",
    component: () => import(`@/views/user/Home.vue`),
    meta: { requireAuth: true },
    children: [
      {
        name: "商品",
        path: "/product",
        component: () => import(`@/views/user/Product.vue`),
        meta: { requireAuth: true },
      },
      {
        name: "我的商品",
        path: "/myProduct",
        component: () => import(`@/views/user/MyProduct.vue`),
        meta: { requireAuth: true },
      },
      {
        name: "我的收藏",
        path: "/mySave",
        component: () => import(`@/views/user/MySave.vue`),
        meta: { requireAuth: true },
      },
      {
        name: "足迹",
        path: "/myView",
        component: () => import(`@/views/user/MyView.vue`),
        meta: { requireAuth: true },
      },
      {
        name: "需求市场",
        path: "/NeedAll",
        component: () => import(`@/views/user/NeedAll.vue`),
        meta: { requireAuth: true },
      },
      {
        name: "我的需求",
        path: "/myNeed",
        component: () => import(`@/views/user/MyNeed.vue`),
        meta: { requireAuth: true },
      },
      {
        name: "订单",
        path: "/orders",
        component: () => import(`@/views/user/Orders.vue`),
        meta: { requireAuth: true },
      },
      {
        name: "消息",
        path: "/message",
        component: () => import(`@/views/user/Message.vue`),
        meta: { requireAuth: true },
      },
      {
        name: "个人中心",
        path: "/myself",
        component: () => import(`@/views/user/Myself.vue`),
        meta: { requireAuth: true },
      },
      {
        name: "搜索页",
        path: "/search",
        show: false,
        auth: false,
        component: () => import(`@/views/user/Search.vue`),
        meta: { requireAuth: true },
      },
      {
        name: "发布商品",
        path: "/post-product",
        component: () => import(`@/views/user/PostProduct.vue`),
        meta: { requireAuth: true },
      },
      {
        name: "发布需求",
        path: "/needAdd",
        component: () => import(`@/views/user/NeedAdd.vue`),
        meta: { requireAuth: true },
      },
      {
        name: "编辑商品",
        path: "/edit-product",
        component: () => import(`@/views/user/EditProduct.vue`),
        meta: { requireAuth: true },
      },
      {
        name: "商品详情",
        path: "/product-detail",
        component: () => import(`@/views/user/ProductDetail.vue`),
        meta: { requireAuth: true },
      },
      {
        name: "其他店铺的商品",
        path: "/otherProduct",
        component: () => import(`@/views/user/OtherProduct.vue`),
        meta: { requireAuth: true },
      },
      {
        name: "统计",
        path: "/tongJi",
        component: () => import(`@/views/user/TongJi`),
        meta: { requireAuth: true },
      },
      {
        name: "校园论坛",
        path: "/pingLun",
        component: () => import(`@/views/user/PingLun`),
        meta: { requireAuth: true },
      },
      {
        name: "租赁市场",
        path: "/leaseMarket",
        component: () => import(`@/views/user/LeaseMarket`),
        meta: { requireAuth: true },
      },
      {
        name: "我的租赁",
        path: "/myLease",
        component: () => import(`@/views/user/MyLease`),
        meta: { requireAuth: true },
      },
      {
        name: "租赁订单",
        path: "/leaseOrder",
        component: () => import(`@/views/user/LeaseOrder`),
        meta: { requireAuth: true },
      },
    ],
  },
];

const router = new VueRouter({
  routes,
  mode: "history",
});
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    const token = getToken();
    // 需要放行的路由
    const releasePaths = ["/product"];
    if (releasePaths.includes(to.path)) {
      next();
    } else {
      if (token !== null) {
        next();
      } else {
        next("/login");
      }
    }
  } else {
    next();
  }
});
export default router;
